package com.x.bff.app.service.customer;

import com.x.bff.app.model.vo.directory.*;
import com.x.core.utils.BeanUtil;
import com.x.provider.api.customer.model.dto.directory.AttributeDTO;
import com.x.provider.api.customer.model.dto.directory.AttributeListReqDTO;
import com.x.provider.api.customer.model.dto.directory.RegionDTO;
import com.x.provider.api.customer.model.dto.directory.RegionListReqDTO;
import com.x.provider.api.customer.service.DirectoryRpcService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DirectoryServiceImpl implements DirectoryService{

    private DirectoryRpcService directoryRpcService;

    public DirectoryServiceImpl(DirectoryRpcService directoryRpcService){
        this.directoryRpcService = directoryRpcService;
    }

    @Override
    public List<RegionVO> listRegion(RegionListReqVO regionListReq) {
        List<RegionDTO> resultDTO = directoryRpcService.listRegion(BeanUtil.prepare(regionListReq, RegionListReqDTO.class)).getData();
        return BeanUtil.prepare(resultDTO, RegionVO.class);
    }

    @Override
    public Map<String, AttributeVO> listAttribute(AttributeListReqVO attributeListReq) {
        Map<String, AttributeDTO> attributeDTOList = directoryRpcService.listAttribute(BeanUtil.prepare(attributeListReq, AttributeListReqDTO.class)).getData();
        Map<String, AttributeVO> result = prepare(attributeDTOList);
        return result;
    }

    private Map<String, AttributeVO> prepare(Map<String, AttributeDTO> attributeDTOList) {
        Map<String, AttributeVO> result = new HashMap<>(attributeDTOList.size());
        attributeDTOList.entrySet().stream().forEach(item -> {
            AttributeVO attributeVO = BeanUtil.prepare(item.getValue(), AttributeVO.class);
            attributeVO.setAttributeValueList(BeanUtil.prepare(item.getValue().getAttributeValueList(), AttributeValueVO.class));
            result.put(item.getKey(), attributeVO);
        });
        return result;
    }
}
